package com.live.http.slider;

import java.io.File;
import java.util.List;

public class LoadContent implements Runnable{

	private ListPageChannel channel;

	public LoadContent(ListPageChannel channel) {
		this.channel = channel;
	}
	
	public void run(){
		
		try {
			loadPic();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void loadPic() throws Exception {
		
		ListPage listPage = channel.takeListPage();
		List<Page> pages=listPage.getPages();
		int pageId=listPage.getPageId();
		String folder;
		
		File file=createFolder(String.valueOf(pageId));
		File file2;
		for(Page page:pages){
			folder=page.getName();
			final String pageUrl=page.getUrl();
			file2=new File(file, folder);
			file2.mkdir();
			final String path=file2.getCanonicalPath();
			
			ThreadPool.execute(new Runnable() {
				
				public void run() {
					try {
						new SaveFile(pageUrl, path).save();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
	}
	
	public File createFolder(String folder)throws Exception{
		File saveFolder=new File(Constants.SAVE_LOCATION,folder);
		if(!saveFolder.exists()){
			saveFolder.mkdir();
		}
		return saveFolder;
	}

	/**
	 * 处理图片的地址
	 * 
	 * @param picUrl
	 * @return
	 */

}
